package com.hepinghospital.user.ui.activity.consult;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hepinghospital.user.R;
import com.hepinghospital.user.base.BaseActivity;
import com.hepinghospital.user.base.BaseApplication;
import com.hepinghospital.user.ui.PayActivity;
import com.hepinghospital.user.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.observer.BaseObserver;
import com.rest.response.Doctor;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import jz.joyoung.robot.util.DpUtil;
import jz.joyoung.robot.util.SoftKeyboardUtil;
import jz.joyoung.robot.util.UIConstants;

/**
 * Created by Administrator on 2020/2/9.
 */

public class DepartmentListActivity extends BaseActivity {
    @BindView(R.id.refreshLayout)
    RefreshLayout refreshLayout;
    @BindView(R.id.doctor_recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.department_recyclerView)
    RecyclerView department_recyclerView;
    @BindView(R.id.edt_search)
    EditText edt_search;

    MyAdapter adapter;
    DepartmentAdapter departmentAdapter;

    List<Doctor> departmentList = new ArrayList<>();
    List<Doctor> doctorList = new ArrayList<>();


    @Override
    protected void initViews() {
        LinearLayoutManager manager = new LinearLayoutManager(DepartmentListActivity.this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        adapter = new MyAdapter();
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDraw(c, parent, state);
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.bottom = DpUtil.dip2px(DepartmentListActivity.this, 16);
            }
        });

        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshListener(refreshlayout -> initData());
        refreshLayout.setOnLoadMoreListener(refreshlayout -> {

        });
        department_recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        departmentAdapter = new DepartmentAdapter();
        department_recyclerView.setAdapter(departmentAdapter);
        edt_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {   // 按下完成按钮，这里和上面imeOptions对应
                    getDepartmentList();
                    department_recyclerView.setVisibility(View.VISIBLE);
                    recyclerView.setVisibility(View.GONE);
                    SoftKeyboardUtil.hideSoftKeyboard(DepartmentListActivity.this);
                }
                return false;
            }
        });
    }

    @Override
    protected void initData() {
        getDepartmentList();
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_department_list;
    }


    class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holder> {

        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            Holder myHolder;
            myHolder = new Holder(LayoutInflater.from(DepartmentListActivity.this).inflate(R.layout.item_doctor, parent, false));
            return myHolder;
        }

        @Override
        public void onBindViewHolder(Holder holder, final int position) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BaseApplication.currentDoctor = doctorList.get(position);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(UIConstants.OBJ_Doctor, doctorList.get(position));
//                    startActivity(PayMinioActivity.class, bundle);
                    startActivity(PayActivity.class, bundle);
                }
            });
            holder.tv_department.setText(doctorList.get(position).deptName);
            holder.tv_level.setText(doctorList.get(position).jobTitleName);
            holder.tv_skill.setText("擅长:" + doctorList.get(position).docGoodAtNames);
            holder.tv_price.setText(doctorList.get(position).fee);
            holder.tv_turn_num.setText(doctorList.get(position).num);
            holder.tv_name.setText(doctorList.get(position).docName);
//            ImageLoaderHelper.getInstance().GlideImageLoader(DepartmentListActivity.this,doctorList.get(position).);
        }

        @Override
        public int getItemCount() {
            return doctorList.size();
        }

        class Holder extends RecyclerView.ViewHolder {

            TextView tv_name, tv_department, tv_level, tv_skill, tv_turn_num, tv_price;
            ImageView img_head;

            public Holder(View itemView) {
                super(itemView);
                tv_name = itemView.findViewById(R.id.tv_name);
                tv_level = itemView.findViewById(R.id.tv_level);
                tv_department = itemView.findViewById(R.id.tv_department);
                tv_skill = itemView.findViewById(R.id.tv_skill);
                tv_turn_num = itemView.findViewById(R.id.tv_turn_num);
                tv_price = itemView.findViewById(R.id.tv_price);
                img_head = itemView.findViewById(R.id.img_head);
            }
        }
    }


    class DepartmentAdapter extends RecyclerView.Adapter<DepartmentAdapter.Holder> {

        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            Holder myHolder;
            myHolder = new Holder(LayoutInflater.from(DepartmentListActivity.this).inflate(R.layout.item_depaterment, parent, false));
            return myHolder;
        }

        @Override
        public void onBindViewHolder(Holder holder, final int position) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getDoctorList(departmentList.get(position).id);
                    department_recyclerView.setVisibility(View.GONE);
                    recyclerView.setVisibility(View.VISIBLE);
                }
            });
            holder.tv_name.setText(departmentList.get(position).name);
        }

        @Override
        public int getItemCount() {
            return departmentList.size();
        }

        class Holder extends RecyclerView.ViewHolder {

            TextView tv_name;
            ImageView img_head;

            public Holder(View itemView) {
                super(itemView);
                tv_name = itemView.findViewById(R.id.tv_name);
            }
        }
    }

    private void getDepartmentList() {
        RestProxy.getInstance().getDepartmentList(edt_search.getText().toString(), new Observer<List<Doctor>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(List<Doctor> doctors) {
                departmentList.clear();
                departmentList.addAll(doctors);
                departmentAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable e) {
                refreshLayout.finishRefresh(false);
                ToastUtil.onError(DepartmentListActivity.this, e);

            }

            @Override
            public void onComplete() {
                refreshLayout.finishRefresh(true);
            }
        });
    }

    private void getDoctorList(String id) {
        RestProxy.getInstance().getDoctorList(id, new BaseObserver<List<Doctor>>() {
            @Override
            public void onError(Throwable e) {
                ToastUtil.onError(DepartmentListActivity.this, e);
            }

            @Override
            public void onSuccess(List<Doctor> result) {
                doctorList.clear();
                doctorList.addAll(result);
                adapter.notifyDataSetChanged();
            }
        });
    }
}
